package com.xsd.awen.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsd.awen.config.App;
import com.xsd.awen.config.WebResult;
import com.xsd.awen.domain.entity.SysApp;
import com.xsd.awen.domain.po.SysAppPagePo;
import com.xsd.awen.domain.vo.SysAppVo;
import com.xsd.awen.mapper.SysAppMapper;
import com.xsd.awen.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

@Service
@Scope(proxyMode= ScopedProxyMode.TARGET_CLASS)
public class SysAppService {

    @Autowired
    SysAppMapper sysAppMapper;

    public PageInfo<SysAppPagePo> page(SysAppVo sysAppVo){
        PageHelper.startPage(sysAppVo.getPage(),sysAppVo.getLimit());
        return new PageInfo<>(sysAppMapper.page(sysAppVo));
    }

    public String save(SysApp sysApp){
        Integer count = sysAppMapper.countSysApp(sysApp);
        if( count != null && count > 0 ){
            return WebResult.ERROR("用户名重复");
        }
        sysApp.setId(UUIDUtils.generate());
        sysAppMapper.insert(sysApp);
        return WebResult.OK();
    }

    public String update(SysApp sysApp){
        Integer count = sysAppMapper.countUpdateSysApp(sysApp);
        if( count != null && count > 0 ){
            return WebResult.ERROR("用户名重复");
        }
        sysAppMapper.updateById(sysApp);
        return WebResult.OK();
    }

    public String delete(SysApp sysApp){
        sysAppMapper.deleteById(sysApp);
        return WebResult.OK();
    }

    public SysApp getById(String id){
        SysApp sysApp = sysAppMapper.selectById(id);
        return sysApp;
    }

}
